package com.feinno.bug.actcache.help;

import java.util.Random;

/**
 * @Author: zcy
 * @Description
 * @Date: 16:24 2018/11/6
 */
public class RandomPSWd {
    /**
     * 随机生成1-10位的密码
     *
     * @param pwdType 密码类型，大写，小写，数字 或三都的混合
     * @param length  生成密码的长度
     * @return 密码字符串
     */
    public String GernaratePWD(PasswordType pwdType, int length) {
        String rtnstr = "";
        try {
            for (int i = 0; i < length; i++) {
                switch (pwdType) {
                    case UpCase:
                        rtnstr += (char) (Math.random() * 26 + 'A');    //生成随机大写字母
                        break;
                    case LowerCase:
                        rtnstr += (char) (Math.random() * 26 + 'a');  //生成随机小写字母
                        break;
                    case Number:
                        rtnstr += String.valueOf((int) (Math.random() * 10));  //生成随机数字
                        break;
                    case Mixed:        //生成随机大写字母、小写字母或数字
                        Random random = new Random();
                        switch (random.nextInt(3)) {
                            case 0:
                                rtnstr += (char) (Math.random() * 26 + 'A');
                                break;
                            case 1:
                                rtnstr += (char) (Math.random() * 26 + 'a');
                                break;
                            case 2:
                                rtnstr += String.valueOf((int) (Math.random() * 10));
                                break;
                            default:
                                break;
                        }
                        break;
                    default:
                        break;
                }
            }

        } catch (Exception e) {
            rtnstr = "";
        }
        return rtnstr;
    }

    public static void main(String[] args) {
        int length = (int)( Math.random()*10 + 1);      // 1-10位
        System.out.println("随机" + length +"位大写字母：" +(new RandomPSWd()).GernaratePWD(PasswordType.UpCase, length));
        System.out.println("随机" + length +"位小写字母：" +(new RandomPSWd()).GernaratePWD(PasswordType.LowerCase, length));
        System.out.println("随机" + length +"位数字：" +(new RandomPSWd()).GernaratePWD(PasswordType.Number, length));
        System.out.println("随机" + length +"位混合密码：" +(new RandomPSWd()).GernaratePWD(PasswordType.Mixed, length));
    }
}
